/**
 * Created by duanxiaofei on 2016/2/3.
 */
;
(function($,window,document,undefined){

    function Hashtable(){
        this._hash={};
    }

    Hashtable.prototype={
        add:function(key,value){
            if (typeof(key) != "undefined") {
                if (this.contains(key) == false) {
                    this._hash[key] = typeof(value) == "undefined" ? null : value;
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        },
        update: function (key, value) {
            if (typeof key != "undefined") {
                this._hash[key] = typeof(value) == "undefined" ? null : value;
                return true;
            } else {
                return false;
            }
        },
        remove: function (key) {
            var re = this._hash[key];
            delete this._hash[key];
            return re;
        },
        count: function () {
            var i = 0;
            for (var k in this._hash) {
                i++;
            }
            return i;
        },
        items: function (key) {
            return this._hash[key];
        },
        contains: function (key) {
            return typeof(this._hash[key]) != "undefined";
        },
        clear: function () {
            for (var k in this._hash) {
                delete this._hash[k];
            }
        },
        keys: function () {
            var re = [];
            for (var k in this._hash) {
                re.push(k);
            }
            return re;
        },
        each: function (handler) {
            if (!jQuery.isFunction(handler)) {
                return;
            }
            for (var k in this._hash) {
                handler(this._hash[k], k);
            }
        },
        toArray: function () {
            var re = [];
            for (var k in this._hash) {
                re.push(this._hash[k]);
            }
            return re;
        },
        addAll: function (arr, each) {
            var num = arr.length;
            for (var i = 0; i < num; i++) {
                var item = each.call(this, arr[i]);
                item && this.add(item[0], item[1]);
            }
        },
        fromObject:function(obj){
            var re=new Hashtable();
            re._hash=obj;
            return re;
        }
    };

    $.Hashtable=function(){
        return new Hashtable();
    };

})(jQuery,window,document);